package cm.shit;

import java.util.Date;

class Th implements Cloneable {
    String s = "233";
    Date d = new Date();

    // 深度拷贝
    @Override
    protected Th clone() throws CloneNotSupportedException {
        // 先调用Object的浅拷贝
        Th cloned = (Th)super.clone();
        // 进一步考本Date对象, Date实现了Cloneable接口
        // 而且重写了clone方法
        // 但是编译器不知道...
        // 所以需要声明这个异常
        cloned.d = (Date)this.d.clone();
        return cloned;
    }
}

public class TestClone {
    public static void main(String[] args) {
        Th t1 = new Th();
        System.out.println("t1.d: " + t1.d);
        try {
            Th t2 = t1.clone();
            t2.d.setTime(666);
            System.out.println("after change t2.d");
            System.out.println("t2.d: " + t2.d);
            System.out.println("t1.d: " + t1.d);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
